﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.Win32;
using Xuhengxiao.MyDataStructure;


namespace 修改ADO判断
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int intN = 8; ;
            try
            {
                 intN = Convert.ToInt32(textBox2.Text);
                
            }
            catch (Exception ex)
            {

                MessageBox.Show("请输入整数,错误原因："+ex.Message);
                return;
            }

            JieJueExcelPanDuanShuZiWenTi(intN);

        }


        /// <summary>
        /// 这个方法只是解决导入EXCEL文件时的判断数据问题，当导入EXCEL文件的时候，他会判断每一列的数据类型，但只是判断前几项，这个方法
        /// 是将这个数值增大，多判断几项，我设置成判断1000项
        /// </summary>
        private void JieJueExcelPanDuanShuZiWenTi(int intNumber)
        {
            //“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel”修改这个中的TypeGuessRows ，设置数值为1000

            try
            {
                //定位最开头的
                RegistryKey key = Registry.LocalMachine;

                RegistryKey reg1 = key.OpenSubKey(@"SOFTWARE\Microsoft\Jet", true);

                //我要用搜索找出这个TypeGuessRows
                RegistryKey TypeGuessRows = getRegistryKey(reg1);

                try
                {
                    string str1 = intNumber.ToString();

                    if (TypeGuessRows.GetValue("TypeGuessRows").ToString() != str1)
                        TypeGuessRows.SetValue("TypeGuessRows", intNumber.ToString());

                    MessageBox.Show("修改成功");

                }
                catch (Exception ex1)
                {
                    ClsErrorFile.WriteLine(ex1);
                    //Console.WriteLine(ex1.Message);
                    //throw;
                }


                key.Close();
                TypeGuessRows.Close();

            }
            catch (Exception ex)
            {
                ClsErrorFile.WriteLine(ex);
                //Console.WriteLine(ex.Message);
                //throw;
            }


        }


        private RegistryKey getRegistryKey(RegistryKey registryKey)
        {

            RegistryKey reg = null;

            try
            {
                //如果这个子项内有"TypeGuessRows"，就表示找到这个子项了。
                if (registryKey.GetValue("TypeGuessRows") != null)
                {
                    return registryKey;
                }

            }
            catch (Exception ex)
            {
                ClsErrorFile.WriteLine("查找TypeGuessRows" , ex);

                //Console.Error.WriteLine(ex.Message);
            }

            try
            {

                //如下是迭代
                ;

                foreach (string item in registryKey.GetSubKeyNames())
                {
                    RegistryKey regtemp = getRegistryKey(registryKey.OpenSubKey(item, true));

                    if (regtemp != null)
                        return regtemp;
                }


            }
            catch (Exception ex)
            {
                ClsErrorFile.WriteLine(ex);
                //Console.Error.WriteLine(ex.Message);

            }

            return reg;


        }
    }
}
